﻿using System;
using System.Web.Mvc;
using AtentoFramework2008.Infrastructure.Entity;
using AtentoFramework2008.Infrastructure.Persistence;
using NStore.Domain;
using NStore.Infrastructure.Repositories;

namespace NStore.Web.Controllers
{
    public class SchemaController : Controller
    {
        public ActionResult Create()
        {
            if (Request.IsAuthenticated)
                ViewData["info"] = "Logout for create schema !!!";
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (!Request.IsAuthenticated)
            {
                ViewData["info"] = "Schema created !!!";

                NHibernateSessionManager.Instance.CreateDatabase();
                User user = new User("resti", "Pa$$w0rd") {Email = "resti@server.com"};
                user.Roles.Add("admin");

                UserRepository userRepository = new UserRepository();
                userRepository.Add(user);


                Repository<Product> productRepository = new Repository<Product>();

                var titles = new[]
                            {
                                "Programming Microsoft ASP.NET MVC",
                                "Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)",
                                "Professional ASP.NET MVC 2 ",
                                "ASP.NET MVC Framework Unleashed",
                                "ASP.Net MVC in Action",
                                "Programming ASP.Net MVC",
                                "Test-Drive ASP.NET MVC"
                            };
                var services = new[]
                            {
                                "Administrative Support",
                                "Developer",
                                "Manager",
                                "Programmer",
                                "Project Director",
                                "Specialist",
                                "Subject Matter Expert",
                                "System Engineer"
                            };

                Random random = new Random();

                foreach (var title in titles)
                {
                    Product book = new Product();
                    book.Name = title;
                    book.Description = "--";
                    book.Price = (float)(30 * random.NextDouble());
                    productRepository.Add(book);
                }

                foreach (var service in services)
                {
                    Product book = new Product();
                    book.Name = service;
                    book.Category = ProductCategory.Servicio;
                    book.Description = "--";
                    book.Price = (float)(30 * random.NextDouble());
                    productRepository.Add(book);
                }
                

            }
            return View();
        }

    }
}
